import time
import simplehtf as htf
from simplehtf.utils import func
from simplehtf.core.launcher import NativeLauncher

class CMDPlug(htf.BasePlug):
    def setup(self):
        print("[Plug] DUT terminal setup")

    def teardown(self):
        print("[Plug] DUT terminal teardown")

    def run_cmd(self,cmd):
        #假装需要一秒
        time.sleep(1)
        import os
        if cmd == 'uname':
            return 'Darwin'
        return f"{cmd}"


@func.matcher('common_command')
@htf.plug(cmd=CMDPlug)
def common_command(test,cmd,args,expected_value ,mode):
    res = cmd.run_cmd(args)
    print(res)
    return htf.PhaseResult.CONTINUE

@func.matcher('common_command_with_expected_value')
@htf.plug(cmd=CMDPlug)
def common_command_with_expected_value(test,cmd,args,expected_value ,mode):
    res = cmd.run_cmd(args)
    print(f"real result is {res}")
    print(f"expected value is {expected_value}")
    if expected_value in res:
        return htf.PhaseResult.CONTINUE
    return 'FAIL'

launcher = NativeLauncher()

#如果你不喜欢我给出的格式 你可以尝试编写自定义格式化器
launcher.load_test_from_csv('./examples/data.csv')
launcher.test_finish_callback(lambda x:print(x.to_dict()))
launcher._poll_for_start(lambda:True)

input()